package com.netty.chapter09;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.logging.LoggingHandler;

public class TestMain {
    public static void main(String[] args) throws Exception {
        EmbeddedChannel channel = new EmbeddedChannel(
                new LoggingHandler(),
                new LengthFieldBasedFrameDecoder(1024,12,4,0,0),
                new CustomMessageCodec()
        );

        TestMessage testMessage=new TestMessage("zhangsan",10);

        // 测试写出数据
      channel.writeOutbound(testMessage);

        // 测试写出数据
        ByteBuf buf= ByteBufAllocator.DEFAULT.buffer();
        new CustomMessageCodec().encode(null,testMessage,buf);
        ByteBuf buf1 = buf.slice(0, 100);
        buf1.retain();
        ByteBuf buf2 = buf.slice(100, buf.readableBytes() - 100);
        channel.writeInbound(buf1);
        channel.writeInbound(buf2);
    }
}
